//
//  SetUpMessBoardVC.m
//  SelfDrivingTravelling-IOS
//
//  Created by Teplot_01 on 16/10/17.
//  Copyright © 2016年 Teplot_01. All rights reserved.
//

#import "SetUpMessBoardVC.h"

@interface SetUpMessBoardVC ()<UITextViewDelegate>

@end

@implementation SetUpMessBoardVC
{
    UILabel *_placeHolderLabel;
    UITextView *_messBoardTV;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = White_Color;
    self.title = @"留言板";
    //取消iOS7的边缘延伸效果（例如导航栏，状态栏等等） TextView光标上下闪动问题
    self.modalPresentationCapturesStatusBarAppearance = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.extendedLayoutIncludesOpaqueBars = NO;
    [self initViewForSetUpMessBoard];
}
- (void)initViewForSetUpMessBoard{
    _messBoardTV = [[UITextView alloc]initWithFrame:CGRectMake(20, 20, KScreenWidth - 40, 200)];
    _messBoardTV.backgroundColor = LighterGrayColor;
    _messBoardTV.delegate = self;
    [self.view addSubview:_messBoardTV];
    
    //placeHolderLabel
    _placeHolderLabel = [[UILabel alloc]initWithFrame:CGRectMake(3, 0, 300, 30)];
    _placeHolderLabel.text = @"在这里编辑您要留言的内容";
    _placeHolderLabel.font = [UIFont systemFontOfSize:13];
    [_placeHolderLabel setTextColor:LightGray_Color];
    [_messBoardTV addSubview:_placeHolderLabel];
    //btn
    UIButton *doneBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth - 100, 230, 70, 30)];
    [doneBtn setTitle:@"留言" forState:UIControlStateNormal];
    [doneBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    doneBtn.layer.cornerRadius = 3;
    doneBtn.backgroundColor = Theme_Color;
    [doneBtn addTarget:self action:@selector(doneBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:doneBtn];
}
#pragma - mark UITextViewDelegate
-(void)textViewDidChange:(UITextView *)textView{
    if (textView.text.length == 0) {
        _placeHolderLabel.text = @"在这里编辑您要留言的内容";
    }else{
        _placeHolderLabel.text = @"";
    }
}
//
- (void)doneBtnAction{
    //参数
    NSDictionary *parameters = @{
                                 @"type": @"1",
                                 @"content": _messBoardTV.text,
                                 @"receiver": self.clubId,
                                 };
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:@"http://115.28.152.130/tour/v1/bbs/create.action" parameters:parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@>>>>",dic);
        NSString *error = dic[@"code"];
        if (![dic[@"code"] isEqualToString:@"0000"]) {
            [SVProgressHUD showErrorWithStatus:[NSString errorStringWithCode:error]];
        }else{
            //创建成功
            [SVProgressHUD showSuccessWithStatus:@"留言成功"];
            [[self navigationController] popViewControllerAnimated:YES];
        }
        
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"留言失败"];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
